//
//  ZYXPThreadViewController.m
//  MultiThreading
//  PThread 多线程技术演示
//

#import "ZYXPThreadViewController.h"

#import <pthread.h>

@interface ZYXPThreadViewController ()

@end

@implementation ZYXPThreadViewController




/**
 *  pthread_create(pthread_t *restrict, const pthread_attr_t *restrict, void *(*)(void *), void *restrict)
 *
 *  pthread_t *restrict 线程id         restrict 限制
 *
 *  *restrict           表示传入地址 指针要传入变量的地址
 *
 *  void *(*)(void *)   指向函数的指针 线程启动，线程是做事情用的，线程要执行任务，你要分配给这条线程执行什么任务，这里使用C函数代表线程要执行的任务
 *                      返回值是     void*
 *                      接收参数是   void*
 *
 *
 *
 */



//要指向的任务
void *run(void *data)
{
    NSLog(@"run...该run函数执行的线程是 %@",[NSThread currentThread]);
    //2015-03-13 23:10:45.556 MultiThreading[14680:3107] run...该run函数执行的线程是 <NSThread: 0x8e00170>{name = (null), num = 2}
    //2015-03-13 23:12:14.306 MultiThreading[14799:3107] run...该run函数执行的线程是 <NSThread: 0x8d02600>{name = (null), num = 2}
    //for (int i=0; i<5000; i++) {
    for (int i=0; i<2; i++) {
        NSLog(@"%d run函数执行线程是 %@",i,[NSThread currentThread]);
    }
    /*
     2015-03-13 23:45:43.463 MultiThreading[17230:607] viewDidLoad方法执行的线程是 <NSThread: 0x8f04eb0>{name = (null), num = 1}
     2015-03-13 23:45:44.285 MultiThreading[17230:3807] run...该run函数执行的线程是 <NSThread: 0x8c194e0>{name = (null), num = 2}
     2015-03-13 23:45:44.286 MultiThreading[17230:3807] 0 run函数执行线程是 <NSThread: 0x8c194e0>{name = (null), num = 2}
     2015-03-13 23:45:44.286 MultiThreading[17230:3807] 1 run函数执行线程是 <NSThread: 0x8c194e0>{name = (null), num = 2}
     2015-03-13 23:45:45.660 MultiThreading[17230:380b] run...该run函数执行的线程是 <NSThread: 0x8c18d80>{name = (null), num = 3}
     2015-03-13 23:45:45.661 MultiThreading[17230:380b] 0 run函数执行线程是 <NSThread: 0x8c18d80>{name = (null), num = 3}
     2015-03-13 23:45:45.662 MultiThreading[17230:380b] 1 run函数执行线程是 <NSThread: 0x8c18d80>{name = (null), num = 3}
     2015-03-13 23:45:46.736 MultiThreading[17230:380f] run...该run函数执行的线程是 <NSThread: 0x8c18d80>{name = (null), num = 4}
     2015-03-13 23:45:46.737 MultiThreading[17230:380f] 0 run函数执行线程是 <NSThread: 0x8c18d80>{name = (null), num = 4}
     2015-03-13 23:45:46.738 MultiThreading[17230:380f] 1 run函数执行线程是 <NSThread: 0x8c18d80>{name = (null), num = 4}
    */
    return NULL;
}


/**
 *  viewDidLoad 方法在主线程调用
 *  run函数在子线程中调用 把耗时的操作放在run函数中执行不会阻塞主线程
 */
-(void)viewDidLoad
{
    [super viewDidLoad];
    
    NSLog(@"viewDidLoad方法执行的线程是 %@",[NSThread currentThread]);
    //2015-03-13 23:13:59.224 MultiThreading[14919:607] viewDidLoad方法执行的线程是 <NSThread: 0x8f084e0>{name = (null), num = 1}
    //2015-03-13 23:13:59.225 MultiThreading[14919:3403] run...该run函数执行的线程是 <NSThread: 0x8d55be0>{name = (null), num = 2}
    
//    pthread_t myRestrict;   //线程id
//    pthread_create(&myRestrict, NULL, run, NULL);
    
}



/**
 *  点击控制器的view做耗时的操作
 *  该方法在主线程执行
 */
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//    for(int i=0;i<4000;i++){
//        NSLog(@"%d   touchesBegan:withEvent: 执行线程是 %@",i,[NSThread currentThread]);
//    }
    
    pthread_t myRestrict;   //线程id
    pthread_create(&myRestrict, NULL, run, NULL);
}

@end
